package com.homepage.service;

import com.homepage.domain.NewsImage;
import com.homepage.repository.NewsImageRepository;
import com.homepage.service.dto.NewsImageDTO;
import com.homepage.service.mapper.NewsImageMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link NewsImage}.
 */
@Service
@Transactional
public class NewsImageService {

    private final Logger log = LoggerFactory.getLogger(NewsImageService.class);

    private final NewsImageRepository newsImageRepository;

    private final NewsImageMapper newsImageMapper;

    public NewsImageService(NewsImageRepository newsImageRepository, NewsImageMapper newsImageMapper) {
        this.newsImageRepository = newsImageRepository;
        this.newsImageMapper = newsImageMapper;
    }

    /**
     * Save a newsImage.
     *
     * @param newsImageDTO the entity to save.
     * @return the persisted entity.
     */
    public NewsImageDTO save(NewsImageDTO newsImageDTO) {
        log.debug("Request to save NewsImage : {}", newsImageDTO);
        NewsImage newsImage = newsImageMapper.toEntity(newsImageDTO);
        newsImage = newsImageRepository.save(newsImage);
        return newsImageMapper.toDto(newsImage);
    }

    /**
     * Partially update a newsImage.
     *
     * @param newsImageDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<NewsImageDTO> partialUpdate(NewsImageDTO newsImageDTO) {
        log.debug("Request to partially update NewsImage : {}", newsImageDTO);

        return newsImageRepository
            .findById(newsImageDTO.getId())
            .map(existingNewsImage -> {
                newsImageMapper.partialUpdate(existingNewsImage, newsImageDTO);

                return existingNewsImage;
            })
            .map(newsImageRepository::save)
            .map(newsImageMapper::toDto);
    }

    /**
     * Get all the newsImages.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<NewsImageDTO> findAll() {
        log.debug("Request to get all NewsImages");
        return newsImageRepository.findAll().stream().map(newsImageMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one newsImage by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<NewsImageDTO> findOne(Long id) {
        log.debug("Request to get NewsImage : {}", id);
        return newsImageRepository.findById(id).map(newsImageMapper::toDto);
    }

    /**
     * Delete the newsImage by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete NewsImage : {}", id);
        newsImageRepository.deleteById(id);
    }
}
